# -*- coding: UTF-8 -*-
from PIL import Image
import numpy as np
import os
import random
import configparser
#number = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
number = []
Alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U','V', 'W', 'X', 'Y', 'Z', '#']

#Other = ['@','!','&','%']
Other = []
char_set = number + Other + Alphabet
def get_image_file_name(imgPath,maxLen):
    fileName = []
    total = 0
    for filePath in os.listdir(imgPath):
        captcha_name = filePath.split('/')[-1]
        if(len(captcha_name) < maxLen):
            print(captcha_name)
            continue
        if(captcha_name.find(",") >= 0):
            print(captcha_name)
            continue
        if(captcha_name.find("#") >= 0):
            print(captcha_name)
            continue
        if(captcha_name.find("0") >= 0):
            print(captcha_name)
            continue
        if(captcha_name.find(";") >= 0):
            print(captcha_name)
            continue
        if(captcha_name.find("[") >= 0):
            print(captcha_name)
            continue
        if(captcha_name.find("$") >= 0):
            print(captcha_name)
            continue
        if(len(captcha_name) > maxLen):
            print(captcha_name)
            continue
        fileName.append(captcha_name)
        total += 1
    return fileName, total

def get_image_file_name_no_verify(imgPath):
    fileName = []
    total = 0
    for filePath in os.listdir(imgPath):
        captcha_name = filePath.split('/')[-1]
        tmpName = captcha_name[0:captcha_name.find('.')]
        fileName.append(captcha_name)
        total += 1
    return fileName,total
def getPos(char_set=char_set, char=None):
    return char_set.index(char)

def text2vec(text,CAPTCHA_LEN,CHAR_SET_LEN):
    text = text.upper()
    text_len = len(text)
    if text_len > CAPTCHA_LEN:
        raise ValueError('验证码最长4个字符')
    vector = np.zeros(CAPTCHA_LEN * CHAR_SET_LEN)
    for i, c in enumerate(text):
        idx = i * CHAR_SET_LEN + getPos(char=c)
        vector[idx] = 1
    return vector


def get_data_and_label(fileName,filePath,CAPTCHA_LEN,CHAR_SET_LEN,MAX_LEN):
    pathName = os.path.join(filePath, fileName)
    img = Image.open(pathName)
    #转为灰度图
    image_array = np.array(img)
    #image_array = np.array(img)
    image_data = 1*(image_array.flatten())
    #image_label = name2label(fileName[0:CAPTCHA_LEN])
    image_label = text2vec(fileName[0:CAPTCHA_LEN],MAX_LEN,CHAR_SET_LEN)
    return image_data, image_label

def del_file(path):
    ls = os.listdir(path)
    for i in ls:
        c_path = os.path.join(path, i)
        if os.path.isdir(c_path):
            del_file(c_path)
        else:
            os.remove(c_path)

def get_test_image_data(file_name):
    image = Image.open(file_name)
    #img_ = image.convert('L')
    #img_ = binarizing(img_,100)
    image_array = np.array(image)
    image_data = 1*(image_array.flatten())
    return image_data

def binarizing(img,threshold):
    pixdata = img.load()
    w, h = img.size
    for y in range(h):
        for x in range(w):
            if pixdata[x, y] < threshold:
                pixdata[x, y] = 0
            else:
                pixdata[x, y] = 255
    return img

def vec2string(vec):
    result = ''
    for i, char in enumerate(vec):
        result += (char_set[char])

    return result

def GetINIData(file_name,section,key):
    conf = configparser.ConfigParser()
    conf.read(file_name)
    result_value = conf.get(section,key)
    return result_value
